package com.atguigu.vod.controller;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.CommonResult;
import com.atguigu.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import com.atguigu.vod.utils.InitVodClient;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author : 李红磊
 * @Date : 2021/06/21/16:17
 * @Description :
 */

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadAlyiVideo")
    public CommonResult uploadAlyiVideo(MultipartFile file) {
        System.out.println("请求打到vodController");
        String videoId = vodService.uploadVIdeo(file);
        System.out.println("vID是"+videoId);
        return CommonResult.ok().data("videoId", videoId);
    }

    //根据视频id删除阿里云视频
    @DeleteMapping("removeVideo/{videoId}")
    public CommonResult removeVideo( @PathVariable String videoId) {

        try {
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);

            DeleteVideoRequest request = new DeleteVideoRequest();//创建删除视频的request对象

            request.setVideoIds(videoId);//将要删的视频id放入
            //调用对象进行删除
            client.getAcsResponse(request);
            return CommonResult.ok().message("视频删除成功");


        }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException("删除视频失败");
        }
    }


    //删除多个阿里云视频方法
    @DeleteMapping("removerPatch")
    public CommonResult deleteBatch(@RequestParam List<String> videoIdList) {
        vodService.removeAllVideo(videoIdList);
        return CommonResult.ok();
    }


    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public CommonResult getPlayAuth(@PathVariable("id")String id){

            System.out.println("11111111111111");
            try {
                //创建初始化对象
                DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
                //创建获取凭证的request和response对象
                GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
                //向request对象中设置视频id
                request.setVideoId(id);

                //调用方法获得凭证
                GetVideoPlayAuthResponse response = client.getAcsResponse(request);
                String playAuth = response.getPlayAuth();
                return CommonResult.ok().data("playAuth", playAuth);
            } catch (ClientException | com.aliyuncs.exceptions.ClientException e) {
                e.printStackTrace();
                throw new GuliException(20001,"视频playAuth获取失败");
            }


    }


}
